var documentHelper = (function (documentUtil) {
    "use strict";

    var module = {};

    var asArray = function (arrayLike) {
        return Array.prototype.slice.call(arrayLike);
    };

    var cascadingAction = {
        active: true,
        hover: true,
        focus: false,
        target: false,
    };

    module.fakeUserAction = function (element, selector, action) {
        var elem = element.querySelector(selector),
            pseudoClass = ":" + action,
            fakeActionClass = "rasterizehtml" + action;
        if (!elem) {
            return;
        }

        if (cascadingAction[action]) {
            documentUtil.addClassNameRecursively(elem, fakeActionClass);
        } else {
            documentUtil.addClassName(elem, fakeActionClass);
        }
        documentUtil.rewriteCssSelectorWith(
            element,
            pseudoClass,
            "." + fakeActionClass
        );
    };

    module.persistInputValues = function (doc) {
        var inputs = doc.querySelectorAll("input"),
            textareas = doc.querySelectorAll("textarea"),
            isCheckable = function (input) {
                return input.type === "checkbox" || input.type === "radio";
            };

        asArray(inputs)
            .filter(isCheckable)
            .forEach(function (input) {
                if (input.checked) {
                    input.setAttribute("checked", "");
                } else {
                    input.removeAttribute("checked");
                }
            });

        asArray(inputs)
            .filter(function (input) {
                return !isCheckable(input);
            })
            .forEach(function (input) {
                input.setAttribute("value", input.value);
            });

        asArray(textareas).forEach(function (textarea) {
            textarea.textContent = textarea.value;
        });
    };

    module.rewriteTagNameSelectorsToLowerCase = function (element) {
        documentUtil.lowercaseCssTypeSelectors(
            element,
            documentUtil.findHtmlOnlyNodeNames(element)
        );
    };

    return module;
})(documentUtil);
